package com.too.service;

import com.too.dao.HugeTableTestMapper;
import com.too.entity.HugeTableTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 测试逻辑
 *
 * @author ouyf
 * @date 2024/10/23 16:42
 * <p>
 * Copyright (c) Siemens Mobility TBDS 2024, All Rights Reserved, Confidential
 */
@Service
public class HugeTableTestService {

    @Autowired
    private HugeTableTestMapper hugeTableTestMapper;

    private ExecutorService executor = Executors.newFixedThreadPool(10);

    public void insertMysql(List<HugeTableTest> list) {
        CountDownLatch countDownLatch = new CountDownLatch(list.size()/1000);
        for (int i = 0; i < list.size(); i = i + 1000) {
            List<HugeTableTest> subList = list.subList(i, i + 1000);
            executor.execute(() -> {hugeTableTestMapper.insertBatch(subList);countDownLatch.countDown();});
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
